<?php
/**
 * Created by JetBrains PhpStorm.
 * User: JKen22078
 * Date: 7/22/13
 * Time: 10:52 PM
 * To change this template use File | Settings | File Templates.
 */
class Upload extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('pagination');
        $this->load->model('upFile');
        $this->load->model('support_file');
        $this->load->library('javascript');
        $this->load->library('javascript/jquery');
    }
    function test(){
        $this->load->view('pages/site/home1');
    }
    public function index()
    {
        $content['child'] = $this->get_file_param(52,1,4,0);
        $content['countItem'] = count($this->get_file_param(52, 1, 10000, 0));
        $content['pagination'] = $this->pagination(52,$content['countItem'],0);
        $data['category'] = $this->sortArray();
        $uri = $this->uri->uri_to_assoc(2);
        if(isset($uri['4'])){

            if($uri['4'] == false){
                $index = 0;
            }else{
                $index = $uri['4'];
            }
        }else{
            $index = 0;
        }
        $content['index'] = $index;
        $content['breakcrum'] = $this->breakcrum(52);
        $this->load->view('pages/site/header');
        $this->load->view('pages/site/navibar');
        $this->load->view('pages/site/sidebar', $data);
        $this->load->view('pages/site/home',$content);
        $this->load->view('pages/site/footer');
    }
    public function data()
    {
    $data = array('updatecat' => site_url('upload/task/update'),
    'set'=>site_url('upload/task/set'),
    'upload'=>site_url('upload/do_upload'));
    $this->load->view('pages/task/summy',$data);
    }
    function getSupportFile(){
        $types = $this->support_file->support_file_type();
        foreach($types as $type){
            $string_type[] = $type->type;
        }
        $all_type = implode('|',$string_type);
       return $all_type;
    }//lay file ho tro
    function do_upload() //them file vao base
    {
        //todo: su ly upload tai day
        if (!$_POST) {
            $loadParent['parent'] = $this->getInfo('1', 'tbl_category');
            $this->load->view('pages/form_upload', $loadParent);
        } else {
            $folder = $this->input->post('slcat', TRUE);
            $subCat = $this->input->post('child', TRUE);
            if ($folder == $subCat) {
                $idCat = $folder;
            } else {
                $idCat = $subCat;
            }
            //var_dump($folder);die();
            $config['upload_path'] = './uploads/' . $idCat;
            $config['allowed_types'] = $this->getSupportFile();
            $this->load->library('upload', $config); //load libary
            if (!$this->upload->do_upload('file')) {
                echo 'upload ko thanh cong ';
                echo '<a href="'.site_url('upload').'">Back</a>';
            } else {
                $data = array('upload_data' => $this->upload->data(), 'idCat' => $idCat);
                $this->upFile->insert_file($data);
                $this->load->view('pages/upload_success', $data);
            }
        }
    } //funtion upload

    function copy_file()
    {
        copy('./uploads/two/blue-add.png', './uploads/one/blue-add-abccc.png');
    }

    function update_file()
    {
        if ($_POST != null) {
            $idCallBack = $this->input->post('idfile', TRUE);
            //$cat = $this->getInfo($idCallBack,'tbl_file');
            $desc = './uploads/' . $this->input->post('parent', TRUE) . '/' . $this->input->post('updatefile', TRUE);
            $source = './uploads/' . $this->input->post('idCat', TRUE) . '/' . $this->input->post('name', TRUE);
            $bCopyOK = copy($source, $desc);
            unlink($source);
            if ($bCopyOK == true) {
                $data = array('idCat' => $this->input->post('parent', TRUE), 'name' => $this->input->post('updatefile', TRUE));
                $updateOK = $this->upFile->update_file($idCallBack, $data); //truyên id và data
            }
        } else {
            echo 'not request';
        }
        /*todo: update file
match lại folder
sửa lại thông tin db
thay dổi folder thì copy sang folfer khac
neu ko de nhu bt chỉ chỉnh thông tin */
        //$data = array('upload_data' => $this->upload->data(),'room'=>$this->input->post('slRoom',TRUE),'slfiletyle'=>$this->input->post('slFileType',TRUE));
        //$this->upFile->update_file($id,$data); //truyên id và data
    }

    function delete_file($id,$idcat,$name){
        $debase = urldecode($name);
        $name_file = base64_decode($debase);
        //$name_file = urldecode($name);
        $source = './uploads/' . $idcat . '/' . $name_file;
        unlink($source);
        $this->upFile->delete_file($id);
    }
    function getStaff($idOffice)
    {
        /*todo: get nhan su cua phong qua id phong*/
    }

    function get($stt = 'view', $idCat = '1', $all = 1, $offset = '4', $start = 0)
    {
        /*todo: get file by office id or project id*/
        $data['view'] = $stt; //view file tạm ổn
        switch ($stt) {
            case 'view':
                $uri = $this->uri->uri_to_assoc(2);
                if(isset($uri['4'])){
                    if($uri['4'] == false){
                        $index = 0;
                    }else{
                        $index = $uri['4'];
                    }
                }else{
                    $index = 0;
                }
                $data['category'] = $this->sortArray();
                $content['countItem'] = count($this->get_file_param($idCat, $all, 10000, 0));
                $content['child'] = $this->get_file_param($idCat, $all, $offset, $start);
                $content['pagination'] = $this->pagination($idCat,$content['countItem'],$start);
                $content['breakcrum'] = $this->breakcrum($idCat);
                $content['index'] = $index;//$this->uri->uri_to_assoc(2);
                $this->load->view('pages/site/header');
                $this->load->view('pages/site/navibar');
                $this->load->view('pages/site/sidebar', $data);
                $this->load->view('pages/site/home',$content);
                $this->load->view('pages/site/footer');
                break;
            case 'update':
                $data['stt'] = $stt;
                $data['menu'] = $this->sortArray();
                $data['item'] = $this->get_item($idCat);
                $this->load->view('pages/task/file', $data);
                break;
        }

    } //view file
    function pagination($idcat,$numberItem,$start,$all=1){
        $config['base_url'] = site_url('upload/get/view/'.$idcat.'/'.$all.'/4/');
        $config['total_rows'] = $numberItem;
        $config['per_page'] = 4;
        $config['cur_page'] = $start;
        $this->pagination->initialize($config);
       return $this->pagination->create_links();
    }
    function breakcrum($idcat){
        $check_child = $this->upFile->getInfo($idcat,'tbl_category'); // kiểm tra xem con là thằng nào
        $check_item = $this->upFile->getItem($idcat,'tbl_category'); // thong tin cat
        $check_item_parent = $this->upFile->getItem($check_item['0']->parent,'tbl_category'); // thong tin cat
        $string_parent = '<a href="'.site_url('upload/get/view/'.$check_item_parent['0']->idCat).'">'.$check_item_parent['0']->nameCat.'</a> > ';
        $string_child = '<a href="'.site_url('upload/get/view/'.$check_item['0']->idCat).'">'.$check_item['0']->nameCat.'</a>';
        if($check_child != null || $check_item['0']->parent == 1){
            //la thang con nen phai tim cha no
            return $string_child;
        }else{
            //la thang cha in luon cha
            return $string_parent.$string_child;
        }
    }
    function get_item($id)
    {
        return $this->upFile->getItem($id, 'tbl_file');
    }
    function get_file_param($idCat, $all = '1', $offset = 50, $start = 0)
    {
        /*if idcat dm cha + $all =1 -> view toanf bo file cha con
        if id dm cha + all = 0 -> view file dm cha
        if id con la con: view file con
        ___
        kt idcat la cha hay con

        */
        $data = $this->getInfo($idCat, 'tbl_category', $offset, $start);
        $items = array();
        $items[] = $idCat;
        if ($all == 1 && $data != null) {
            foreach ($data as $itemData) {
                $items[] = $itemData->idCat;
            }
        }
        $file = $this->upFile->getInfo($items, 'tbl_file', $offset, $start);
        return $file;
    }

    /*todo: doạn dưới insert thông tin category vào DB*/
    function task($stt = 'update', $param = '')
    {
        /*todo: call insert category*/
        switch ($stt) {
            case 'set':
                $STT['stt'] = $stt;
                $STT['menu'] = $this->sortArray();
                //$STT['cat_exist']= $this->getInfo();
                $this->load->view('pages/task/task', $STT);
                break;
            case 'update':
                $STT['stt'] = $stt;
                $STT['menu'] = $this->sortArray();
                $this->load->view('pages/task/task', $STT);
                break;
            case 'item':
                $STT['stt'] = $stt;
                $STT['menu'] = $this->sortArray();
                $STT['item'] = $this->getItem($param, 'tbl_category');
                $this->load->view('pages/task/task', $STT);
                break;
            case 'delete':
                $STT['stt'] = $stt;
                $STT['delete'] = $this->deleteCat($param, 'tbl_category');
                $this->load->view('pages/task/task', $STT);
                break;
            default:
                break;
        }
    }
    function sortArray()
    {
        $arr = $this->upFile->getmenu();

        if ($arr == null) {
            return false;
        } else {
            foreach ($arr as $key => $value) {
                if ($value->parent == '1') {
                    $arrmenu[$value->idCat][0] = $value;
                }
                foreach ($arr as $key1 => $value1) {
                    if ($value->idCat == $value1->parent) {
                        $arrmenu[$value->idCat][1][$value1->idCat] = $value1;
                    }
                }
            }
            return $arrmenu;
        }
    }

    function setInfo( /*$nameCat,$parent = 1,*/
        $tbl = 'tbl_category')
    {
        $nameCat = $this->input->post('nameCat');
        $parent = $this->input->post('idCat');
        /*todo: set danh muc*/
        switch ($tbl) {
            case 'tbl_support':
                $dataCat = array('type' => $nameCat);
                break;
            default:
                $STT['stt'] = 'set';
                $dataCat = array('nameCat' => $nameCat, 'parent' => $parent);
                //echo site_url('upload/task/',$STT);
                header('location: ' . site_url('upload/task/set'));
                break;
        }
        $this->setInfoDB($tbl, $dataCat);
    }
    function setInfoDB($tbl, $data)
    { //$tbl la ban can truyen
        $insetOk = $this->upFile->setCat($tbl, $data); //id cuoi cung
        //var_dump($insetOk);die();
        if ($tbl == 'tbl_category') {
            $this->makeFolder($insetOk);
        } else {
        }
    }
    /*todo: update category*/
    function updateCat( /*$idCat,$nameCat,$parent,*/$order = 0)
    {
        $idCat = $this->input->post('idCat');
        $nameCat = $this->input->post('updateName');
        $parent = $this->input->post('parent', '1');
        //$this->db->where('idCat',$idCat);
        $this->upFile->updateCat($idCat, $nameCat, $parent, $order);
    }

    /*todo: xóa category*/
    function deleteCat($idCat, $tbl)
    {
        $deleteOK = $this->upFile->deleteCat($idCat, $tbl);
        return $deleteOK;
    }

    function getInfo($paren = 1, $tbl = 'tbl_category', $offset = 50, $start = 0)
    {
        /*todo: lay thong tin 1 danh muc*/
        $tblInfo['tblInfo'] = $this->upFile->getInfo($paren, $tbl, $offset, $start);
        return $tblInfo['tblInfo'];
    }
    function getInfoAjax($paren = 1, $tbl = 'tbl_category')
    {
        /*todo: lay thong tin 1 danh muc*/
        $tblInfo = $this->upFile->getInfo($paren, $tbl);
        echo '<option value="' . $paren . '">--subroot--</option>';
        foreach ($tblInfo as $child) {
            echo '<option value="' . $child->idCat . '">' . $child->nameCat . '</option>';
        }
    }
    function getItem($id, $tbl)
    {
        return $this->upFile->getItem($id, $tbl);
    }

    function makeFolder($folder)
    {
        mkdir('./uploads/' . $folder, 0777);
    }
}